iT邦幫忙

2025 iThome 鐵人賽

DAY 8
0

前言

延續著Day05的專案我們來一步一步了解maven的架構及使用

POM structure

POM是Project Object Model的簡寫為專案物件模型,前一日提及maven是由Java撰寫的構建工具,所以既然要用程式來做事情,一定須要設定檔能夠讓我們進行各式各樣的task,讓我們攤開Day05的pom檔搭配官網的分類,逐步往下看

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>

  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>

  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>

maven座標

它定義了三個維度(groupId、artifactId、version)讓我們可以定位到專案的獨一無二性,就像是門牌地址一樣

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  • groupId:公司名稱加上專案名稱
  • artifactId:英文翻譯是人工製品,手工藝品,對應到軟體的世界通常就是指專案名稱或是模組名稱
  • version:這個就很直覺了,就是版本號
    例如我們在springframework專案中會看到,org.springframework,底下有spring-core、spring-aop、spring-beans指的就是不同的模組名稱
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>6.2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>6.2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>6.2.11</version>
</dependency>

我們來看一個底下沒有分拆模組的esapi會是這樣定義它的artifactId

<!-- https://mvnrepository.com/artifact/org.owasp.esapi/esapi -->
<dependency>
    <groupId>org.owasp.esapi</groupId>
    <artifactId>esapi</artifactId>
    <version>2.7.0.0</version>
</dependency>

packaging

我們在Day05的專案中沒有此標籤,預設的就是jar所以才未出現,如果是web的專案這邊可以改為war進行打包

dependencies

這個部分就是放我們相依jar檔的地方,你只要給他maven座標(groupId、artifactId、version),它就會去預設的repoitory下載,就像我們在day05專案中寫入相關的套件

 <dependencies>
    <!-- logback -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.5.18</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>2.0.17</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

Properties

maven也提供了很多預先定義好的設定讓我們調用

  1. env:調用環境變數
    例如:${env.PATH}
  2. project:調用專案相關變數
    例如:${project.basedir}專案根目錄
  3. settings:調用settings.xml中相關變數
    例如:false,可以設定maven離線模式不連線至遠端倉庫
  4. Java System Properties:可以透過java.lang.System.getProperties()取得之properties
    例如:${java.home}、${os.name}、${java.version}
  5. 自訂義變數
    例如我們在寫springframework相關套件的version時會發現,他們通常都是同一個版本號,我們可以透過properties設置讓dependencies透過${},參考如下
<properties>
    <springframework.version>6.2.11</springframework.version>
</properties>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${springframework.version}</version>
</dependency>
<!-- 略 -->

dependencyManagement

查看一下先前的例子,套件管理實際並不下載套件,僅是作為dependencies中的管理或是方便於管理子模組

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.11.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <!-- 由套件管理定義好相關的版本,我們在下方就不需要再加入版本號 -->
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- Optionally: parameterized tests support -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-params</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

build

這邊定義了構建時所需要執行task的plugin訊息

<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  <plugins>
    <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
    <plugin>
      <artifactId>maven-clean-plugin</artifactId>
      <version>3.4.0</version>
    </plugin>
    <!-- 其他略 -->
</pluginManagement>

More Project Information

關於設置專案的相關訊息,例如Licenses、Organization、Developers、Contributors、專案名稱、專案網址等訊息,請參考官網設定方式

reference


上一篇
Day06 - Maven Intruction
系列文
我們與Maven的距離8
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言